package com.hjm.project.riskControl.riskmerblack.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.merchantInfo.merchantInfo.mapper.MerchantInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.riskControl.riskmerblack.mapper.RiskmerblackMapper;
import com.hjm.project.riskControl.riskmerblack.domain.Riskmerblack;
import com.hjm.project.riskControl.riskmerblack.service.IRiskmerblackService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商户黑名单Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-11-03
 */
@Service
public class RiskmerblackServiceImpl implements IRiskmerblackService 
{
    @Resource
    private RiskmerblackMapper riskmerblackMapper;

    @Resource
    private MerchantInfoMapper merchantInfoMapper;

    /**
     * 查询商户黑名单
     * 
     * @param id 商户黑名单ID
     * @return 商户黑名单
     */
    @Override
    public Riskmerblack selectRiskmerblackById(Integer id)
    {
        return riskmerblackMapper.selectRiskmerblackById(id);
    }

    /**
     * 查询商户黑名单列表
     * 
     * @param riskmerblack 商户黑名单
     * @return 商户黑名单
     */
    @Override
    public List<Riskmerblack> selectRiskmerblackList(Riskmerblack riskmerblack)
    {
        return riskmerblackMapper.selectRiskmerblackList(riskmerblack);
    }

    /**
     * 查询商户黑名单列表
     *
     * @param riskmerblack 商户黑名单
     * @return 商户黑名单 一个结果
     */
    @Override
    public List<Riskmerblack> selectRiskmerblackOne(Riskmerblack riskmerblack)
    {
        return riskmerblackMapper.selectRiskmerblackOne(riskmerblack);
    }

    /**
     * 新增商户黑名单
     * 
     * @param riskmerblack 商户黑名单
     * @return 结果
     */
    @Override
    public int insertRiskmerblack(Riskmerblack riskmerblack)
    {
        return riskmerblackMapper.insertRiskmerblack(riskmerblack);
    }

    /**
     * 修改商户黑名单
     * 
     * @param riskmerblack 商户黑名单
     * @return 结果
     */
    @Override
    public int updateRiskmerblack(Riskmerblack riskmerblack)
    {
        return riskmerblackMapper.updateRiskmerblack(riskmerblack);
    }

    /**
     * 删除商户黑名单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRiskmerblackByIds(String ids)
    {
        return riskmerblackMapper.deleteRiskmerblackByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户黑名单信息
     * 
     * @param id 商户黑名单ID
     * @return 结果
     */
    @Override
    public int deleteRiskmerblackById(Integer id) {
        return riskmerblackMapper.deleteRiskmerblackById(id);
    }

    /**
     * 导入文件
     * @Param [id]
     * @Return int
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int importFile(List<Riskmerblack> list){
        List<Riskmerblack> lists = new ArrayList<>();
        for (Riskmerblack data : list) {
            //查看是否已录入
            Riskmerblack info = new Riskmerblack();
            info.setType("2");
            info.setTypeVal(data.getTypeVal());
            List<Riskmerblack> riskmerblacks = riskmerblackMapper.selectRiskmerblackOne(info);
            if (CollectionUtils.isNotEmpty(riskmerblacks)) {
                throw new BusinessException("请检查数据,当前数据已存在:" + data.getTypeVal());
            }
            data.setStatus("0");
            data.setAddDate(DateUtils.getDate());
            data.setAddMan(ShiroUtils.getLoginName());

            if (StringUtils.isNotEmpty(data.getMerNum())) {
                lists.add(data);
            }
        }

        int i = riskmerblackMapper.insertRiskmerblackBatch(list);
        return i;
    }
}
